package day12;

import java.sql.Time;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: 王丹
 * @create: 2021-08-03 22:30
 **/
public class Test {
    private static final Lock lock = new ReentrantLock();
    private static int num = 0;

    public static void add(String name) {
        try {
            lock.lock();
            num++;
            System.out.println(name + "-----");
        } finally {
            lock.unlock();
        }
    }

    public static class T extends Thread {
        private String name;
        public T(String name){
            this.name = name;
        }
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 1000; i++) {
                add(name);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T t1 = new T("t1");
        T t2 = new T("t2");
        //T t3 = new T();

        t1.start();
        t2.start();
        // t3.start();

        System.out.println("1***" + System.currentTimeMillis());
        t1.join();
        System.out.println("2***" + System.currentTimeMillis());
        t2.join();
        System.out.println("3***" + System.currentTimeMillis());
//        t3.join();


        System.out.println(num);
    }
}
